import { ref, toRefs } from "vue";
import useDictStore from "@/store/modules/dict";
import { selectDictDataByType } from "@/apis/dict/dict";

export function useDict(...args) {
  const res = ref({});
  return (() => {
    args.forEach(dictType => {
      res.value[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        selectDictDataByType(dictType)
          .then(resp => {
            res.value[dictType] = resp.data.map(p => ({
              label: p.dictLabel,
              value: p.dictValue
            }));
            useDictStore().setDict(dictType, res.value[dictType]);
          })
          .catch(err => {
            console.log(err);
          });
      }
    });
    return toRefs(res.value);
  })();
}
